//
//  QHCommonUtil.m
//  NewsFourApp
//
//  Created by chen on 14/8/9.
//  Copyright (c) 2014年 chen. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import "QHCommonUtil.h"

//#import <zipzap/zipzap.h>
#import "ZipArchive.h"

@implementation QHCommonUtil

+ (UIImage *)getImageFromView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

+ (UIColor *)getRandomColor
{
    return [UIColor colorWithRed:(float)(1+arc4random()%99)/100 green:(float)(1+arc4random()%99)/100 blue:(float)(1+arc4random()%99)/100 alpha:1];
}

/*0--1 : lerp( float percent, float x, float y ){ return x + ( percent * ( y - x ) ); };*/
+ (float)lerp:(float)percent min:(float)nMin max:(float)nMax
{
    float result = nMin;
    
    result = nMin + percent * (nMax - nMin);
    
    return result;
}


/**
 *	@brief	16进制颜色(html颜色值)字符串转为UIColor
 *
 *	@param 	stringToConvert 	16进制颜色字符串
 *
 *	@return	返回转换后颜色，如果异常数据，返回白色
 */
+(UIColor *) hexStringToColor: (NSString*) stringToConvert

{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    
    if ([cString length] < 6) return [UIColor blackColor];
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor blackColor];
    
    // Separate into r, g, b substrings
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

/**
 *	@brief	根据当前设置的主题，通过图片名称获取图片资源
 *
 *	@param 	name 	图片名称
 *
 *	@return	图片资源
 */
+ (UIImage *)imageNamed:(NSString *)name
{
    //主题路径
    NSString *documentsDirectory = [NSString stringWithFormat:@"%@%@",DE.mainPath,@"Resourse"];
    
    UIImage *image = nil;
    if ([QHConfiguredObj defaultConfigure].themefold != nil && [QHConfiguredObj defaultConfigure].themefold.length > 0)
    {
        NSString *path = [[documentsDirectory stringByAppendingPathComponent:[QHConfiguredObj defaultConfigure].themefold] stringByAppendingPathComponent:name];
        image = [UIImage imageWithContentsOfFile:path];
    }
    if (image == nil)
    {
        image = [UIImage imageNamed:name];
    }
    
    return image;
}

/**
 *	@brief	获取配置的颜色
 *
 *	@param 	name 	配置参数，比如：Resourse.lableTitle.textColor
 *
 *	@return	返回配置的颜色，如果配置异常，返回白色
 */
+(UIColor *) colorNamed:(NSString *)name
{
    UIColor *color = [self hexStringToColor:[[QHConfiguredObj defaultConfigure] getValue:name]];
    return color;
}


/**
 *	@brief	获取配置的值
 *
 *	@param 	name 	配置参数，比如：Resourse.loginname
 *
 *	@return	<#return value description#>
 */
+ (NSString*) stringName:(NSString*)name

{
    NSString* value = [[QHConfiguredObj defaultConfigure] getValue:name];
    return value;
}

@end
